package com.vrv.netmgr.database.dao;

import java.sql.SQLException;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.vrv.common.GlobalConstants;
import com.vrv.netmgr.EdpApplication;
import com.vrv.netmgr.database.table.AlarmStatistics;

/**
 * 数据库访问操作 审计报警·本地报警数据·本地区域报警数据统计
 * 
 * @author zxb
 * 
 */
public class AlarmStatisticsDao {
	private static final String TAG = AlarmStatisticsDao.class.getSimpleName();
	private static final boolean DEBUG = true && GlobalConstants.DEBUG;

	private static Dao<AlarmStatistics, Integer> mDao;

	public static Dao<AlarmStatistics, Integer> getmDao() {
		try {
			mDao = EdpApplication.getsEdpApp().getDatabaseHelper().getDao(AlarmStatistics.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mDao;
	}

	/**
	 * 更新或者添加操作
	 * 
	 * @param alarmStatistics
	 */
	public static void insert(AlarmStatistics alarmStatistics) {
		if (DEBUG) Log.v(TAG, "insert()");
		
		try {
			if (null != alarmStatistics) {
				//清理表
				deleteAll();
				//添加新记录
				getmDao().create(alarmStatistics);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除所有记录
	 */
	public static void deleteAll(){
		if (DEBUG) Log.v(TAG, "deleteAll()");
		
		String sql = "DELETE FROM " + "AlarmStatistics".toLowerCase();
		SQLiteDatabase db = EdpApplication.getsEdpApp().getSQLiteDatabase();
		db.execSQL(sql);
	}
	
	
	/**
	 * 查询本地数据库测试
	 * 
	 * @return
	 */
	public static String select() {
		if (DEBUG) Log.v(TAG, "select()");
		
		String response = "";
		
		try {
			List<AlarmStatistics> list = getmDao().queryForAll();
			if (null != list && list.size() > 0) {
				AlarmStatistics al = list.get(0);
				
				String head = "{"
							+ "success:" + true + ","
						    + "totalRecords:" + 0 + ","
							+ "records:" + "null" + ",";
				
				String data = "record:"
							+ "{" 
							+ "sbbh:" + al.getSbbh() + ","
							+ "ipbh:" + al.getIpbh() + ","
							+ "ttxz:" + al.getTtxz() + ","
							+ "llyc:" + al.getLlyc() + ","
							+ "ffwl:" + al.getFfwl() + ","
							+ "zdbj:" + al.getZdbj()
							+"}";
				
				String tail = "}";
				
				//
				response = head + data + tail;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (DEBUG) Log.v(TAG, "select()... response = " + response );
		
		return response;
	}

	/**
	 * SQLiteDatabase操作数据库 可以从全局应用获取SQLiteDatabase引用
	 * 
	 * @param arr
	 */
	public static void insert(String[] arr) {
		SQLiteDatabase db = EdpApplication.getsEdpApp().getSQLiteDatabase();
		db.insert(null, null, null);
	}
}
